<?php
use_helper('Javascript');

function get_url($uri, $params, $function = 'getUrl')
{
  $function = sprintf('%s(\'%s\'', $function, url_for($uri));
  foreach ($params as $key => $param)
  {
  	$function .= sprintf(', \'%s=%s\'', $key, $param);
  }
  
  $function .= ')';

  return $function;
}

function link_to_get_url($name, $uri, $params, $html_options = array())
{
  $function = get_url($uri, $params);
	
  return link_to_function($name, $function, $html_options);
}

function button_to_get_url($name, $uri, $params, $html_options = array())
{
  $function = get_url($uri, $params);
	
  return button_to_function($name, $function, $html_options);
}

function div_button_to($name, $internal_uri, $options = array())
{
  $html_options = _parse_attributes($options);
  
  $onclick = sprintf('document.location.href = \'%s\' ', url_for($internal_uri));
  $html_options['onclick'] = $onclick;
  
  $html_options = _convert_options_to_javascript($html_options);
  
  if (!isset($html_options['class']))
  {
  	$html_options['class'] = 'button';
  }
  
  return content_tag('div', $name, $html_options);
}

function div_button_to_function($name, $javascript, $options = array())
{
  $html_options = _parse_attributes($options);
  
  $html_options = _convert_options_to_javascript($html_options);
  
  $html_options['onclick'] = $javascript;
  
  if (!isset($html_options['class']))
  {
  	$html_options['class'] = 'button';
  }
  
  return content_tag('div', $name, $html_options);
}

function div_button_to_get_url($name, $uri, $params, $options = array(), $function = 'getUrl')
{
  $javascript = get_url($uri, $params, $function);

  return div_button_to_function($name, $javascript, $options);
}

function div_button_to_move($name, $uri, $params, $options = array())
{
  $javascript = get_url_to_move($uri, $params);

  return div_button_to_function($name, $javascript, $options);
}



function get_entrance_route($module = null)
{
  $module = $module ? $module : sfContext::getInstance()->getRequest()->getParameter('module');
  
  return Toolkit::getEntranceRoute($module);
}

function get_php_array_to_js($name, $array)
{
  $js = sprintf('var %s = Array();', $name);
  foreach ($array as $key => $value)
  {
  	$js .= sprintf('%s[%s] = "%s";', $name, $key, $value);
  }
  
  return $js;
}

function get_route_name($uri)
{
  $route_name = trim($uri, '@');
  if (($pos = strpos($route_name, '?')) !== false)
  {
    $route_name = substr($route_name, 0, $pos);
  }
  
  return $route_name;
}


function get_slug_from_name($name)
{
  return Toolkit::getSlugFromName($name);
}

function get_objective_vars($vars)
{
  $new_vars = array();
  $vars_to_unset = array
                   (
                     'this', 
                     '_sfFile', 
                     'sf_type', 
                     'sf_context', 
                     'sf_request', 
                     'sf_params', 
                     'sf_response', 
                     'sf_user', 
                     'sf_data'
                   );
  
  foreach ($vars as $var => $value)
  {
  	if (!in_array($var, $vars_to_unset))
  	{
  	  $new_vars[$var] = $value;
  	}
  }
  
  return $new_vars;
}
//david tataje
function js_function_change_discount_type()
{
  $discountTypes = Doctrine::getTable('DiscountType')->findAll();

  $funct = javascript_tag
           (
             sprintf
             (
               '
                 function changeDiscountType(id)
                 { 
                   %s 
                   %s
                   $("offer_store_product_discount_description").value = id == "" ? "" : descriptions[id];
                   var type = "";
                   if (types[id] == "PD" || types[id] == "GP")
                   {
                     type = "  %%";
                   }
                   else if (types[id] == "GM" || types[id] == "MD" || types[id] == "PI" || types[id] == "PF")
                   {
                     type = "  $";
                   }
                    
                   $("amount_postfix").innerHTML = type;
                 }
               
                 changeDiscountType($("offer_store_product_discount_type_id").value);
               ',
               get_php_array_to_js('descriptions', Toolkit::getArrayFromObjects($discountTypes, 'getId', 'getDescription')),
               get_php_array_to_js('types'       , Toolkit::getArrayFromObjects($discountTypes, 'getId', 'getType'))
             )
           );
           
  return $funct;
}

function format_date($date, $format = 'm-d-Y')
{
  return Toolkit::formatDate($date, $format);
}
